<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Javascript数组详解 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,数组,">
  

  <meta name="description" content="数组的定义  数组是按序号排列的一组值，每个值的位置都有编号（从0开始）。数组本质上是一种特殊的对象。它的键名是按（0，1，2...）排列的一组数字">
<meta name="keywords" content="JavaScript,数组">
<meta property="og:type" content="article">
<meta property="og:title" content="Javascript数组详解">
<meta property="og:url" content="http://blog.poetries.top/2016/12/13/js-arr/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="数组的定义  数组是按序号排列的一组值，每个值的位置都有编号（从0开始）。数组本质上是一种特殊的对象。它的键名是按（0，1，2...）排列的一组数字">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://segmentfault.com/img/bVzkqq">
<meta property="og:updated_time" content="2020-08-15T04:25:31.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Javascript数组详解">
<meta name="twitter:description" content="数组的定义  数组是按序号排列的一组值，每个值的位置都有编号（从0开始）。数组本质上是一种特殊的对象。它的键名是按（0，1，2...）排列的一组数字">
<meta name="twitter:image" content="https://segmentfault.com/img/bVzkqq">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#数组的定义"><span class="toc-text">数组的定义</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#创建数组："><span class="toc-text">创建数组：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#判断比是否是个数组"><span class="toc-text">判断比是否是个数组</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#增加数组元素"><span class="toc-text">增加数组元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#删除数组中的元素"><span class="toc-text">删除数组中的元素</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#类数组对象"><span class="toc-text">类数组对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#数组的遍历"><span class="toc-text">数组的遍历</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#for…in-循环"><span class="toc-text">for…in 循环</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#for-循环和-while-循环"><span class="toc-text">for 循环和 while 循环</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#forEach-方法"><span class="toc-text">forEach()方法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#数组常用的方法"><span class="toc-text">数组常用的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#join-将数值转换为字符串"><span class="toc-text">join() 将数值转换为字符串</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#reverse-将数组逆序"><span class="toc-text">reverse() 将数组逆序</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#sort-数组排序"><span class="toc-text">sort() 数组排序</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#slice-返回部分数组"><span class="toc-text">slice() 返回部分数组</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#splice-数组拼接"><span class="toc-text">splice() 数组拼接</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#isArray-判断是否是数组"><span class="toc-text">isArray() 判断是否是数组</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#indexOf-lastIndexOf-数组检索"><span class="toc-text">indexOf() lastIndexOf() 数组检索</span></a></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-js-arr" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">Javascript数组详解</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2016.12.13</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.1k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 4分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h4 id="数组的定义"><a href="#数组的定义" class="headerlink" title="数组的定义"></a>数组的定义</h4><hr>
<ul>
<li>数组是按序号排列的一组值，每个值的位置都有编号（从<code>0</code>开始）。数组本质上是一种特殊的对象。它的键名是按（<code>0，1，2...</code>）排列的一组数字<a id="more"></a>
</li>
</ul>
<h5 id="创建数组："><a href="#创建数组：" class="headerlink" title="创建数组："></a>创建数组：</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(values);</span><br><span class="line"><span class="keyword">var</span> arr = [vaules];</span><br></pre></td></tr></table></figure>
<h5 id="判断比是否是个数组"><a href="#判断比是否是个数组" class="headerlink" title="判断比是否是个数组"></a>判断比是否是个数组</h5><hr>
<ul>
<li><p><code>Array.isArray(arr)</code></p>
</li>
<li><p><code>arr instanceof Array</code></p>
</li>
</ul>
<h5 id="增加数组元素"><a href="#增加数组元素" class="headerlink" title="增加数组元素"></a>增加数组元素</h5><hr>
<ul>
<li><code>push()</code>方法 在数组的末尾增加一个或多个元素，并返回数组的新长度。</li>
<li><code>unshift()</code>方法 在数组的开头增加一个或多个元素，并返回数组的新长度。</li>
<li><code>length</code> 属性</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">arr.push(<span class="number">4</span>)</span><br><span class="line">arr  <span class="comment">// 1, 2, 3, 4</span></span><br><span class="line">arr.unshift(<span class="number">6</span>)</span><br><span class="line">arr  <span class="comment">// 6, 1, 2, 3, 4</span></span><br><span class="line">arr[arr.length] = <span class="number">7</span>  <span class="comment">// 与push()方法类似</span></span><br><span class="line">arr  <span class="comment">// 6, 1, 2, 3, 4, 7</span></span><br></pre></td></tr></table></figure>
<h5 id="删除数组中的元素"><a href="#删除数组中的元素" class="headerlink" title="删除数组中的元素"></a>删除数组中的元素</h5><hr>
<ul>
<li><code>delete</code> 运算符，可以删除数组中的某个元素，但这不会改变<code>length</code>属性的值.</li>
<li><code>pop()</code> 方法 删除数组的最后一个元素，并返回这个元素</li>
<li><code>shift()</code> 方法 删除数组的第一个元素，并返回这个元素</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">delete</span> arr[<span class="number">0</span>];</span><br><span class="line">arr   <span class="comment">// [undefined,2,3]</span></span><br><span class="line">arr.length  <span class="comment">// 3</span></span><br><span class="line"><span class="keyword">var</span> last = arr.pop()</span><br><span class="line"><span class="keyword">var</span> first = arr.shift()</span><br><span class="line">last <span class="comment">// 3</span></span><br><span class="line">first <span class="comment">// undefined</span></span><br><span class="line">arr <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<h4 id="类数组对象"><a href="#类数组对象" class="headerlink" title="类数组对象"></a>类数组对象</h4><hr>
<ul>
<li>在<code>js</code>中，有些对象被叫做“类数组对象<code>”（array-like object）</code>，因为这些对象看起来很像数组，可以使用<code>length</code>属性，但是无法使用数组的方法。 </li>
<li>典型的类数组对象是函数的<code>arguments</code>对象，以及大多数<code>DOM</code>元素集，还有字符串</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// arguments对象</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">args</span>(<span class="params"></span>) </span>&#123;<span class="keyword">return</span> <span class="built_in">arguments</span>; &#125;</span><br><span class="line"><span class="keyword">var</span> arraylike = args(<span class="string">'a'</span>,<span class="string">'b'</span>)</span><br><span class="line">arrayLike[<span class="number">0</span>]  <span class="comment">// 'a'</span></span><br><span class="line">arrayLike.length <span class="comment">// 2</span></span><br><span class="line">arrayLike <span class="keyword">instanceof</span> <span class="built_in">Array</span> <span class="comment">// false</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(arrayLike)  <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// DOM元素集</span></span><br><span class="line"><span class="keyword">var</span> elts = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'p'</span>);</span><br><span class="line">elts.length  <span class="comment">// 3</span></span><br><span class="line">eles <span class="keyword">instanceof</span> <span class="built_in">Array</span>  <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//字符串</span></span><br><span class="line"><span class="string">'abc'</span>[<span class="number">1</span>]  <span class="comment">// 'b'</span></span><br><span class="line"><span class="string">'abc'</span>.length  <span class="comment">// 3</span></span><br><span class="line"><span class="string">'abc'</span> <span class="keyword">instanceof</span> <span class="built_in">Array</span>  <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<h4 id="数组的遍历"><a href="#数组的遍历" class="headerlink" title="数组的遍历"></a>数组的遍历</h4><hr>
<h5 id="for…in-循环"><a href="#for…in-循环" class="headerlink" title="for…in 循环"></a>for…in 循环</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a =[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">a.other = <span class="string">'other'</span>;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i <span class="keyword">in</span> arr)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log( arr[i]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 1, 2, 3, other</span></span><br></pre></td></tr></table></figure>
<ul>
<li>从上面的输出结果可以看出，利用<code>for..in</code>循环会将动态添加的非数字键的值遍历出来，因此需要使用的时候需要注意</li>
</ul>
<h5 id="for-循环和-while-循环"><a href="#for-循环和-while-循环" class="headerlink" title="for 循环和 while 循环"></a>for 循环和 while 循环</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// for循环</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; a.length; i++) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a[i]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// while循环</span></span><br><span class="line"><span class="keyword">var</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span> (i &lt; a.length) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a[i]);</span><br><span class="line">  i++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> l = a.length;</span><br><span class="line"><span class="keyword">while</span> (l--) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a[l]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="forEach-方法"><a href="#forEach-方法" class="headerlink" title="forEach()方法"></a>forEach()方法</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//array.forEach(callback[, thisArg])</span></span><br><span class="line"><span class="comment">//callback 在数组的每一项上执行的函数，接受三个参数：item: 数组当前项的值，index: 当前项的索引，arr:数组本身。</span></span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">arr.forEach(<span class="function"><span class="keyword">function</span>(<span class="params">item, index, arr</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(item, index);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//1 0</span></span><br><span class="line"><span class="comment">//2 1</span></span><br><span class="line"><span class="comment">//3 2</span></span><br></pre></td></tr></table></figure>
<h4 id="数组常用的方法"><a href="#数组常用的方法" class="headerlink" title="数组常用的方法"></a>数组常用的方法</h4><hr>
<p><img src="https://segmentfault.com/img/bVzkqq" alt></p>
<h5 id="join-将数值转换为字符串"><a href="#join-将数值转换为字符串" class="headerlink" title="join() 将数值转换为字符串"></a>join() 将数值转换为字符串</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">arr.join(); <span class="comment">// "1,2,3"</span></span><br><span class="line">arr.join(<span class="string">"_"</span>); <span class="comment">// "1_2_3"</span></span><br></pre></td></tr></table></figure>
<h5 id="reverse-将数组逆序"><a href="#reverse-将数组逆序" class="headerlink" title="reverse() 将数组逆序"></a>reverse() 将数组逆序</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 原数组会被修改</span></span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">arr.reverse(); <span class="comment">// [3, 2, 1]</span></span><br><span class="line">arr; <span class="comment">// [3, 2, 1]</span></span><br></pre></td></tr></table></figure>
<h5 id="sort-数组排序"><a href="#sort-数组排序" class="headerlink" title="sort() 数组排序"></a>sort() 数组排序</h5><hr>
<ul>
<li>默认情况下是升序排列的，底层是调用了每个数组项的 <code>toString()</code> 方法，然后比较得到字符串，即使每个数组项的数值是数字，比较的也是字符串</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 原数组会被修改</span></span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">12</span>, <span class="number">213</span>, <span class="number">1432</span>, <span class="string">'a'</span>];</span><br><span class="line">arr.sort(); <span class="comment">// [1, 12, 1432, 213, "a"]</span></span><br><span class="line">arr.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> b-a; <span class="comment">//按倒序排列数组</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h5 id="slice-返回部分数组"><a href="#slice-返回部分数组" class="headerlink" title="slice() 返回部分数组"></a>slice() 返回部分数组</h5><hr>
<ul>
<li><code>slice</code>用于复制数组，复制完后旧数组不变，返回得到的新数组是旧数组的子集</li>
<li>第一个参数begin是开始复制的位置，需要注意的是，可以设负数。设负数表示从尾往前数几个位置开始复制</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//原数组不会被修改</span></span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">arr.slice(); <span class="comment">//[1, 2, 3, 4, 5]</span></span><br><span class="line">arr.slice(<span class="number">1</span>,<span class="number">3</span>); <span class="comment">// [2, 3]</span></span><br><span class="line">arr.slice(<span class="number">1</span>, <span class="number">-1</span>); <span class="comment">// [2, 3, 4]</span></span><br><span class="line">arr; <span class="comment">// [1, 2, 3, 4, 5]</span></span><br></pre></td></tr></table></figure>
<h5 id="splice-数组拼接"><a href="#splice-数组拼接" class="headerlink" title="splice() 数组拼接"></a>splice() 数组拼接</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//原数组会被修改</span></span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="comment">//从第三个数组元素删除</span></span><br><span class="line">arr.splice(<span class="number">2</span>); <span class="comment">// returns [3, 4, 5] </span></span><br><span class="line">arr; <span class="comment">// [1, 2]</span></span><br><span class="line"><span class="comment">//从第三个数组元素删除，删除两个元素</span></span><br><span class="line">arr.splice(<span class="number">2</span>, <span class="number">2</span>) <span class="comment">// returns [3, 4]</span></span><br><span class="line">arr; <span class="comment">// [1, 2, 5]</span></span><br><span class="line"><span class="comment">//将'a','b'替换到数组的第二个元素</span></span><br><span class="line">arr.splice(<span class="number">1</span>, <span class="number">1</span>, <span class="string">'a'</span>, <span class="string">'b'</span>)</span><br></pre></td></tr></table></figure>
<h5 id="isArray-判断是否是数组"><a href="#isArray-判断是否是数组" class="headerlink" title="isArray() 判断是否是数组"></a>isArray() 判断是否是数组</h5><hr>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [];</span><br><span class="line"><span class="keyword">var</span> a = <span class="string">"not array"</span>;</span><br><span class="line"><span class="built_in">Array</span>.isArray(arr); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(a); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<h5 id="indexOf-lastIndexOf-数组检索"><a href="#indexOf-lastIndexOf-数组检索" class="headerlink" title="indexOf() lastIndexOf() 数组检索"></a>indexOf() lastIndexOf() 数组检索</h5><hr>
<ul>
<li>两者都用于返回项目的索引值。区别是<code>indexOf</code>从头开始找，<code>lastIndexOf</code>从尾开始找。如果查找失败，无匹配，返回<code>-1</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>];</span><br><span class="line">arr.indexOf(<span class="string">'c'</span>);  <span class="comment">// 2 找到返回数组下标</span></span><br><span class="line">arr.indexOf(<span class="string">'c'</span>, <span class="number">3</span>); <span class="comment">// -1 指定从3号位开始查找</span></span><br><span class="line">arr.indexOf(<span class="string">'f'</span>); <span class="comment">// -1 没找到该元素</span></span><br><span class="line">arr.lastIndexOf(<span class="string">'c'</span>); <span class="comment">// 2</span></span><br><span class="line">arr.lastIndexOf(<span class="string">'c'</span>,<span class="number">2</span>); <span class="comment">// 2</span></span><br><span class="line">arr.lastIndexOf(<span class="string">'f'</span>); <span class="comment">// -1 没找到该元素</span></span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2016/12/13/js-event-listener/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2016/12/13/call-apply-bind/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
